package macrochip.vison.com.gps.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.fh.lib.PlayInfo;
import com.photoalbum.entity.Setting;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.vison.baselibrary.activity.VideoPlayFilterActivity;
import com.vison.baselibrary.activity.VideoPlayH264Activity;
import com.vison.baselibrary.base.BaseActivity;
import com.vison.baselibrary.base.BaseApplication;

import java.lang.ref.WeakReference;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import macrochip.vison.com.gps.BuildConfig;
import macrochip.vison.com.gps.R;
import macrochip.vison.com.gps.application.MyApplication;

public class WelcomeActivity extends BaseActivity {

    private static final int GET_PLAY_INFO = 1031;
    private static final int GET_CONNECT_INFO = 1032;
    private boolean isFirst = true;
    private final MyHandler myHandler = new MyHandler(WelcomeActivity.this);
    @Bind(R.id.version_tv)
    TextView versionTv;

    private static class MyHandler extends Handler {
        private final WeakReference<WelcomeActivity> mActivity;

        public MyHandler(WelcomeActivity activity) {
            mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            WelcomeActivity activity = mActivity.get();
            if (activity != null) {
                switch (msg.what) {
                    case GET_PLAY_INFO:
                        if (null != activity.versionTv) {
                            activity.versionTv.setText("app ver: " + BuildConfig.VERSION_NAME);


                            if (null != PlayInfo.firmwareVer && !PlayInfo.firmwareVer.isEmpty()) {
                                activity.versionTv.append("\nfirmware ver: " + PlayInfo.firmwareVer);
                            }

                        }

                        activity.myHandler.sendEmptyMessageDelayed(GET_PLAY_INFO, 500);
                        break;

                    case GET_CONNECT_INFO:
                        if(!MyApplication.isDeviceConnected()){
                            if(activity.isFirst){
                                activity.isFirst = false;
                                activity.startActivity(new Intent(activity,ConnectActivity.class));
                            }
                        }
                        break;
                }
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setFullscreen();
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);

        myHandler.sendEmptyMessage(GET_PLAY_INFO);
    }

    @Override
    protected void onResume() {
        super.onResume();
        myHandler.removeMessages(GET_CONNECT_INFO);
        myHandler.sendEmptyMessageDelayed(GET_CONNECT_INFO, 1500);
    }



    @OnClick({R.id.btn_video, R.id.button_start,R.id.btn_help})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_help:
                startActivity(new Intent(WelcomeActivity.this,HelpActivity.class));
                break;

            case R.id.video:
                break;

            case R.id.button_start:
                new RxPermissions(WelcomeActivity.this)
                        .request( Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                Manifest.permission.READ_EXTERNAL_STORAGE)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean aBoolean) throws Exception {
                                if (aBoolean) {
                                    BaseApplication.getInstance().initMainSaveFilePath();
                                    Intent intent = new Intent(WelcomeActivity.this,ControlActivity.class);
                                    startActivity(intent);
                                } else {
                                    Toast.makeText(getContext(), "Error! Please open permissions", Toast.LENGTH_LONG).show();
                                }
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                Toast.makeText(getContext(), "Error! Please open permissions", Toast.LENGTH_LONG).show();
                            }
                        });
                break;
        }
    }
}
